$(function () {
	$('.sales-get-info').click(function () {
		var $elem = $(this);
		var sales_id = $elem.children().first().text();
		
		var request = {
				'sales_id' : sales_id
		};
		
		$("#loading-box").dialog("open");
		
		var url = getUrl('complaint', 'getSalesInfo');
		
		$.post(url, request, function (response) {
			var obj = jQuery.parseJSON(response);
			
			$('#sales_info_results').html("");
			
			$('#sales_info_id').html(obj.sales_id);
			
			for (var i = 0; i < obj.sales_details.length; i++) {
				var sales_details = obj.sales_details;
				
				var div = $('<div></div>').addClass('sales-info-table-row');
				
				var sales_detail_id = sales_details[i].sales_details_id;
				
				var hiddenAttr = {
						type : 'hidden',
						id : 'sales_detail_id_' + sales_detail_id,
						name : 'sales_detail_id'
				};
				
				var hidden = $('<input />').attr(hiddenAttr).val(sales_detail_id);
				var col1 = $('<div></div>').addClass('sales-info-table-col1').text(sales_details[i].sales_details_product_name);
				var col2 = $('<div></div>').addClass('sales-info-table-col2').text(sales_details[i].sales_details_total_count);
				var col3 = $('<div></div>').addClass('sales-info-table-col3').text(sales_details[i].sales_details_total_price);
				var clear_both = $('<div></div>').addClass('clear-both');
				
				$(div).append(hidden);
				$(div).append(col1);
				$(div).append(col2);
				$(div).append(col3);
				$(div).append(clear_both);
				
				$(div).click(function () {
					getSalesDetail(this);
				});
				
				$('#sales_info_results').append(div);								
			}
				
			$("#loading-box").dialog("close");
		});
	});
	
	$('#complaint_box_dialog').dialog({
		 autoOpen: false,
		 modal: true,
		 minHeight: 115,
		 width: 455
	});
	
	$( ".button-jquery-ui" )
		 .button()
		 .click(function( event ) {
		 event.preventDefault();
	});
	
	$('.add-product').click(function() {
		var elem = $(this);
		var counterElem = $(elem).parent().prev();
		var qtyHidden = $(elem).parent().siblings().filter('#complaint_box_product_qty_hidden'); 
		var counter = $(counterElem).text();
		var maxValue =$('#complaint_box_sales_detail_max').val();

		counter = parseInt(counter);
		maxValue = parseInt(maxValue);

		if (counter < maxValue) {		
			counter += 1;
	
			$(counterElem).text(counter);
			$(qtyHidden).val(counter);
		}
	 });

	 $('.minus-product').click(function() {
		 var elem = $(this);
		var counterElem = $(elem).parent().prev();
		var qtyHidden = $(elem).parent().siblings().filter('#complaint_box_product_qty_hidden'); 
		var counter = $(counterElem).text();
	
		counter = parseInt(counter);

		counter -= 1;

		if (counter < 0) { 
			counter = 0;
		}			

		$(counterElem).text(counter);
		$(qtyHidden).val(counter);
	 });
	 
	 $('#complaint_button').click(function() {
		 var $sales_detail_id = $('#complaint_box_sales_detail_id').val();
		 var $product_price = $('#complaint_box_product_price').text();
		 var $product_count = $('#complaint_box_product_qty_hidden').val();
		 var $reason = $('#complaint_comments').val();
		 
		 $product_price = parseFloat($product_price);
		 
		 $product_price = $product_price * $product_count;
		 		 
		 var request = {
				 'sales_detail_id' : $sales_detail_id,
				 'product_count' : $product_count,
				 'product_price' : $product_price,
				 'reason' : $reason
		 };
		 
		 $.post('index.php?c=complaint&m=saveComplaint', request, function (response) {
			 //var obj = jQuery.parseJSON(response);
		 });
		 
		 $('#complaint_box_dialog').dialog('close');
		 
		 location.href = getUrl('complaint', 'index');
	 });
});

function getSalesDetail(elem) {
	var sales_detail_id = $(elem).children().filter('[name="sales_detail_id"]').val();
	
	var request = {
			'sales_detail_id' : sales_detail_id
	};
	
	var url = getUrl('complaint', 'getSalesDetail');
	
	$.post(url, request, function (response) {
		var obj = jQuery.parseJSON(response);
		
		$('#complaint_box_product_qty').text(obj.sales_detail_qty);
		$('#complaint_box_product_name').text(obj.product_name);
		$('#complaint_box_product_price').text(obj.product_price);
		$('#complaint_box_product_stock').text(obj.product_qty);
		$('#complaint_box_sales_detail_id').val(obj.sales_detail_id);
		$('#complaint_box_sales_detail_max').val(obj.sales_detail_qty);
		$('#complaint_box_product_qty_hidden').val(obj.sales_detail_qty);
		
		$('#complaint_box_dialog').dialog('open');
	});		
}